package com.yin.bank.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yin.bank.biz.BankBiz;
import com.yin.bank.biz.BizException;
import com.yin.bank.entity.Account;
import com.yin.bank.entity.Oprecord;
import jakarta.servlet.http.HttpSession;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bank")
// 构造
@AllArgsConstructor
public class BankController {
    private final BankBiz bankBiz;

//    public BankController(BankBiz bankBiz) {
//        this.bankBiz = bankBiz;
//    }

    /**
     * 登录
     * @param account
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("/login")
    public Object login(String account, String password, HttpSession session) {
        try {
            Account login = bankBiz.login(account, password);
            session.setAttribute("loginedAccount", login);
            return Map.of("code",200, "msg", "登录成功");
        } catch (BizException e) {
            e.printStackTrace();
            return Map.of("code",400, "msg", e.getMessage());
        }
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public Object logout(HttpSession session) {
        try {
            session.removeAttribute("loginedAccount");
            return Map.of("code",200, "msg", "退出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Map.of("code",500, "msg", "系统错误");
        }
    }

    /**
     * 查询账户信息
     * @param session
     * @return
     */
    @RequestMapping("/info")
    public Object info(HttpSession session) {
        try {
            Account loginedAccount = (Account) session.getAttribute("loginedAccount");
            if (loginedAccount == null) {
                return Map.of("code",400, "msg", "请先登录");
            }
            return Map.of("code",200, "msg", "已登录", "account", loginedAccount);
        } catch (Exception e) {
            e.printStackTrace();
            return Map.of("code",500, "msg", "系统错误");
        }
    }

    /**
     * 转账
     * @param toAccount
     * @param money
     * @param session
     * @return
     */
    @RequestMapping("transfer")
    public Object transfer(Long toAccount, Double money, HttpSession session) {
        try {
            Account loginedAccount = (Account) session.getAttribute("loginedAccount");
            if (loginedAccount == null) {
                return Map.of("code",400, "msg", "请先登录");
            }
            bankBiz.transfer(loginedAccount.getId(), toAccount, money, "转账");
            return Map.of("code",200, "msg", "转账成功");
        } catch (BizException e) {
            e.printStackTrace();
            return Map.of("code",400, "msg", e.getMessage());
        }
    }

    /**
     * 存款
     * @param money
     * @param session
     * @return
     */
    @RequestMapping("/deposit")
    public Object deposit(Double money, HttpSession session) {
        try {
            Account loginedAccount = (Account) session.getAttribute("loginedAccount");
            if (loginedAccount == null) {
                return Map.of("code",400, "msg", "请先登录");
            }
            bankBiz.deposit(loginedAccount.getId(), money, "存款");
            return Map.of("code",200, "msg", "存款成功");
        } catch (BizException e) {
            e.printStackTrace();
            return Map.of("code",400, "msg", e.getMessage());
        }
    }

    /**
     * 取款
     * @param money
     * @param session
     * @return
     */
    @RequestMapping("/withdraw")
    public Object withdraw(Double money, HttpSession session) {
        try {
            Account loginedAccount = (Account) session.getAttribute("loginedAccount");
            if (loginedAccount == null) {
                return Map.of("code",400, "msg", "请先登录");
            }
            bankBiz.withdraw(loginedAccount.getId(), money, "取款");
            return Map.of("code",200, "msg", "取款成功");
        } catch (BizException e) {
            e.printStackTrace();
            return Map.of("code",400, "msg", e.getMessage());
        }
    }

    @RequestMapping("/pay")
    public Object pay(String type, Long account, Double money, HttpSession session) {
        try {
            Account loginedAccount = (Account) session.getAttribute("loginedAccount");
            if (loginedAccount == null) {
                return Map.of("code",400, "msg", "请先登录");
            }
            bankBiz.transfer(loginedAccount.getId(), account, money, type);
            return Map.of("code",200, "msg", "缴费成功");
        } catch (BizException e) {
            e.printStackTrace();
            return Map.of("code",400, "msg", e.getMessage());
        }
    }

    @RequestMapping("queryByPage")
    public Object queryByPage(Integer current, Integer size, HttpSession session) {
        try {
            Account loginedAccount = (Account) session.getAttribute("loginedAccount");
            if (loginedAccount == null) {
                return Map.of("code",400, "msg", "请先登录");
            }
            Page<Oprecord> oprecordPage = bankBiz.queryByPage(loginedAccount.getId(), current, size);
            List<Oprecord> records = oprecordPage.getRecords();
            long total = oprecordPage.getTotal();
            return Map.of("code",200, "msg", "查询成功", "list", Map.of("records", records, "total", total));
        } catch (Exception e) {
            e.printStackTrace();
            return Map.of("code",500, "msg", "系统错误");
        }
    }


}
